# 金铲铲英雄根据玩家等级随机五张英雄牌库的概率算法研究
import random

import requests
import json

# 展示及选择卡牌数量为 5 张
displayed_card_count = 5
# 展示的卡牌库
displayed_card_hero = []

# 玩家各等级获取各星级卡牌对应的概率
player_get_hero = {
    1: {
        1: 1.0,
        2: 0.0,
        3: 0.0,
        4: 0.0,
        5: 0.0
    },
    2: {
        1: 1.0,
        2: 0.0,
        3: 0.0,
        4: 0.0,
        5: 0.0
    },
    3: {
        1: 0.75,
        2: 0.25,
        3: 0.0,
        4: 0.0,
        5: 0.0
    },
    4: {
        1: 0.55,
        2: 0.3,
        3: 0.15,
        4: 0.0,
        5: 0.0
    },
    5: {
        1: 0.45,
        2: 0.33,
        3: 0.2,
        4: 0.02,
        5: 0.0
    },
    6: {
        1: 0.3,
        2: 0.4,
        3: 0.25,
        4: 0.05,
        5: 0.0
    },
    7: {
        1: 0.19,
        2: 0.3,
        3: 0.4,
        4: 0.1,
        5: 0.01
    },
    8: {
        1: 0.18,
        2: 0.27,
        3: 0.32,
        4: 0.2,
        5: 0.03
    },
    9: {
        1: 0.15,
        2: 0.2,
        3: 0.25,
        4: 0.3,
        5: 0.1
    },
    10: {
        1: 0.05,
        2: 0.1,
        3: 0.2,
        4: 0.4,
        5: 0.25
    }
}
# 各星级对应的英雄
price_hero = {
    1: [],
    2: [],
    3: [],
    4: [],
    5: [],
}


def get_hero_json():
    res = requests.get('https://game.gtimg.cn/images/lol/act/jkzlk/js//12/13.4.17-S13/chess.js')
    data = res.json()
    with open('chess.json', 'w') as f:
        f.write(json.dumps(data))


def get_chess_data():
    with open("chess.json", "r") as f:
        data = json.load(f)
        return data


def organization_data():
    data = get_chess_data()
    data = data["data"]
    for v in data.values():
        if v['heroType'] == '0' and v['price'] != '0' and not v['name'].endswith('-变成的迅捷蟹'):
            if v['name'] not in price_hero[int(v['price'])]:
                price_hero[int(v['price'])].append(v['name'])


def get_displayed_card_hero(current_hero_probability):
    # 根据概率随机5个卡牌等级
    for _ in range(1, displayed_card_count + 1):
        count = random.uniform(0, 1)
        tmp_count = 0
        for k, v in current_hero_probability.items():
            tmp_count += v
            if count < tmp_count:
                card = random.choice(price_hero[k])
                displayed_card_hero.append({card: k})
                break


def player_get_hero_algo():
    player_leave = 6  # 玩家等级
    entrance_hero = player_leave  # 可上场英雄数量
    current_hero_probability = player_get_hero[player_leave]  # 当前英雄概率
    print('当前英雄等级：{}'.format(player_leave))
    print('当前英雄池刷新概率：{}'.format(current_hero_probability))
    get_displayed_card_hero(current_hero_probability)
    displayed_card_hero.sort(key=lambda x: list(x.values())[0])

    display_cards = [k for x in displayed_card_hero for k, v in x.items()]
    return display_cards


def start(displayed_card_hero_list):
    # 1、获取所有英雄数据
    # get_hero_json()
    # 2. 组织数据
    organization_data()
    # 3.设计算法
    display_card = player_get_hero_algo()
    print('当前刷出卡牌池为：{}'.format(display_card))
    displayed_card_hero_list = [{k: str(v) + ' 费卡'} for _ in displayed_card_hero_list for k, v in _.items()]
    print('当前刷出卡牌对应星级为：{}'.format(displayed_card_hero_list))


if __name__ == '__main__':
    for i in range(20):
        displayed_card_hero.clear()
        print('-' * 25, f'第 {i + 1} 次', '-' * 25)
        start(displayed_card_hero)
